package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: your name
 * @Date: 2025/2/24 13:09
 */


@Slf4j
@RestController
public class DeptController {

    //private static final Logger log = LoggerFactory.getLogger(DeptController.class);//定义日志记录器


    @Autowired
    private DeptService deptService;

    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list(){
        log.info("查询全部的部门数据");
        List<Dept> deptList = deptService.findAll();
        return Result.success(deptList);
    }

    @Log
    @DeleteMapping("/depts")
    public Result delete(Integer id){
        log.info("根据Id删除:" + id);
        deptService.deleteById(id);
        return Result.success();
    }
    @Log
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){ //从外界输入 dept 为 json 格式 所以要用@PostMapping
        log.info("新增部门:{}",dept);
        deptService.add(dept);
        return Result.success();
    }
    @Log
    @GetMapping("/depts/{id}")
    public Result getInfo(@PathVariable Integer id){//提取id
        log.info("根据ID查询部门:{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){ //jason 结构的请求数据封装到 depts中要收用
        log.info("修改部门:{}", dept);
        deptService.update(dept);
        return Result.success();

    }
}
